var udpModule = require('nl.icept.network.udp');

var udpSocket = udpModule.createUDPSocket();
	
var button = Ti.UI.createButton({
	height: '100px',
	left: '20px',
	right: '20px',
	
	title: 'Send "Hello World"'
});

Ti.UI.currentWindow.add(button);

button.addEventListener('click', function() {
	//udpSocket.enableBroadcast(); //enable this to send packets to 255.255.255.255
	udpSocket.write("127.0.0.1", 12345, "Hello World!");
	
	(function readResponse() {
		if (udpSocket.isReadable()) {
			udpSocket.read(function(packet) {
				alert(packet.data);
			});
		}
		else {
			//Only do a retry, when we didnt receive something yet
			setTimeout(readResponse, 1000);
		}
	})();
});